//
//  ZNTabBarItem.m
//  ZNTabBarController
//
//  Created by Sniper on 15/12/22.
//  Copyright © 2015年 Sniper. All rights reserved.
//

#import "ZNTabBarItem.h"

@interface ZNTabBarItem ()

@property (nonatomic, strong)UIImage *selectedItemImage;
@property (nonatomic, strong)UIImage *unselectedItemImage;
@property (nonatomic, strong)UIImage *selectedBackgroundImage;
@property (nonatomic, strong)UIImage *unselectedBackgroundImage;

@end

@implementation ZNTabBarItem

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initZNTabbarItem];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initZNTabbarItem];
    }
    return self;
}

- (void)initZNTabbarItem {
    self.backgroundColor = [UIColor clearColor];
//    self.tabbarTitle = @"";
//    self.tabbarTitleAttribute = [NSDictionary dictionaryWithObjectsAndKeys:NSFontAttributeName, [UIFont systemFontOfSize:12], nil];
}

/**
 * Only override drawRect: if you perform custom drawing.
 * An empty implementation adversely affects performance during animation.
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGRect frameSize = self.bounds;
//    if (self.isMiddleItem) {
//    frameSize = CGRectMake(frameSize.origin.x, frameSize.origin.y, frameSize.size.width, frameSize.size.height);
//    }
    UIImage *itemImage = nil;
    UIImage *backgroundImage = nil;
    if ([self isSelected]) {
        itemImage = self.selectedItemImage;
        backgroundImage = self.selectedBackgroundImage;
    } else {
        itemImage = self.unselectedItemImage;
        backgroundImage = self.unselectedBackgroundImage;
    }
    [backgroundImage drawInRect:self.bounds];
    CGFloat originX = (frameSize.size.width - itemImage.size.width) / 2;
    CGFloat originY = (frameSize.size.height - itemImage.size.height) / 2;
    NSLog(@"index, originX : %f, originY : %f", originX, originY);
    if (self.isMiddleItem) {
        [itemImage drawInRect:CGRectMake(originX, frameSize.origin.y, itemImage.size.width, itemImage.size.height)];
    } else {
        [itemImage drawInRect:CGRectMake(originX, originY, itemImage.size.width, itemImage.size.height)];
    }

}

- (void)setSelectedItemImage:(UIImage *)selectedImage
         unselectedItemImage:(UIImage *)unselectedImage {
    if (selectedImage) {
        self.selectedItemImage = selectedImage;
    } else {
        self.selectedItemImage = unselectedImage;
    }
    
    if (unselectedImage) {
        self.unselectedItemImage = unselectedImage;
    }
}

- (void)setSelectedBackgroundImage:(UIImage *)selectedImage
         unselectedBackgroundImage:(UIImage *)unselectedImage {
    if (selectedImage) {
        self.selectedBackgroundImage = selectedImage;
    }
    if (unselectedImage) {
        self.unselectedBackgroundImage = unselectedImage;
    }
}

@end
